//@author A0105602X
#include "Item.h"

bool Item::checkEmpty(){
	if ( name == "")
		return true;
	return false;
}

void Item::setCategory(string newcategory){
	category = newcategory;
}

string Item::getName(){
	return name;
}

void Item::writefile(char* filename)
{
	int i;
	ofstream outfile(filename,ios::app);
	outfile << fixed << setprecision(2);
	outfile << name ;
	for (i = 0; i < maxnlength + 1 - name.length(); i ++)   //fiil the rest with white space
		outfile << ' ';
	outfile << price << ' ' << category << ' ' << date << ' ';
	outfile.flush();
	outfile.close();
}

void Item::print()
{
	cout << '|' ;
	cout << std::left << setw(maxnlength + 2) << name ;
	cout << fixed << setprecision(2);
	cout << '|' << std::left << setw(10) << price << endl;
	cout << "    |" << std::left << setw(20) << category;
	cout << '|' << Helper::dayString(date);
}

bool Item::compare(Item& newSpec)
{
	if (name == newSpec.name)
		if (price == newSpec.price)
			if (category == newSpec.category)
                if (date == newSpec.date)
					return true;
	return false;
}

double Item::getPrice(){
	return price;
}

string Item::getVenue(){
	return venue;
}

string Item::getPayer(){
	return payer;
}

string Item::getCategory(){
	return category;
}

string Item::getDate(){
	return date;
}


bool Item::record()
{
	if (!recordName()) return false;
	if (!recordPrice()) return false;
	if (!recordDate()) return false;
	if (!recordExtra()) return false;
	return true;
}

bool Item::recordName()
{
	cout << "Name:\n";                       
	getline(cin, name);               
	if (name == "" || name == "\n") getline(cin,name); //in case the last "\n" is not read.       
	while ( name.length() > 30) {    //check name
		cout << "Length of name should not exceed "<< maxnlength << "! Press Enter key to exit.\n";
		getline(cin,name);
		if (name == "") return false;
	}
	return true;
}

bool Item::recordPrice()
{
	cout << "Price:\n";
	string temp;
	getline(cin,temp);        //in case price is not a number.
	stringstream stream(temp);
	stream >> price;          //use stringstream to extract price
	while ( stream.fail() || price < 0 ) {
		cout << "Price should be a non-negtive number! Input -1 to exit.\n";
		getline(cin,temp);
		if (temp == "-1") return false;
		stream.str( std::string() );
		stream.clear();          //reset stream
		stream << temp;
		stream >> price;
	}
	return true;
}

bool Item::recordDate()
{
	cout << "Date:\n";
	getline(cin,date);

	while (true) {
		if (!Helper::checkDate(date)) {        //check date
			cout << "Input 0 to exit.\n";
			getline(cin,date);
			if (date == "0") return false;
		}
		else return true;
	}
	return true;
}

void Item::record_from_file(ifstream& infile, streampos address)
{
	infile.seekg(address);
	int i;
	char c,last;
	name = "";
	last = ' ';
	for (i = 0; i < maxnlength; i ++) {    //extract maxnlength characters
		infile.get(c);
		if (! ((last == c) && (c == ' '))) name = name + c;   //do not need redundant white space
		last = c;
	}
	if (name[name.length() - 1] == ' ') name.erase(name.end() - 1); 
	infile >> price;
    infile >> category;
    infile >> date;
	infile.get(c);
}
